{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   }
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一元线性回归\n",
    "data = np.genfromtxt(\"data.csv\", delimiter=\",\")# 载入数据\n",
    "x_data = data[:,0]\n",
    "y_data = data[:,1]\n",
    "plt.scatter(x_data,y_data)\n",
    "plt.show()\n",
    "x_data = data[:,0,np.newaxis]\n",
    "y_data = data[:,1,np.newaxis] # sklearn 传的是一个矩阵\n",
    "model = LinearRegression()# 创建并拟合模型\n",
    "model.fit(x_data, y_data)\n",
    "# 画图\n",
    "plt.plot(x_data, y_data, 'b.')\n",
    "plt.plot(x_data, model.predict(x_data), 'r')\n",
    "plt.show()\n",
    "# LinearRegression 可以是多元回归 x_data 矩阵\n",
    "# model.coef_ 为系数\n",
    "# model.intercept_ 为系数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多项式回归：<br>\n",
    "$Y_i = \\theta _0 +  \\theta _1 x^1 + \\cdots \\theta _n x^n$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义多项式回归,degree的值可以调节多项式的特征\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "poly_reg  = PolynomialFeatures(degree=5) \n",
    "# 特征处理\n",
    "x_poly = poly_reg.fit_transform(x_data)\n",
    "# 定义回归模型\n",
    "lin_reg = LinearRegression()\n",
    "# 训练模型\n",
    "lin_reg.fit(x_poly, y_data)  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import linear_model\n",
    "alphas_to_test = np.linspace(0.001, 1)\n",
    "# 创建模型，保存误差值\n",
    "model = linear_model.RidgeCV(alphas=alphas_to_test, store_cv_values=True)\n",
    "model.fit(x_data, y_data)\n",
    "print(model.alpha_)# 岭系数\n",
    "# loss值\n",
    "print(model.cv_values_.shape)# 岭系数\n",
    "# 岭系数跟loss值的关系\n",
    "plt.plot(alphas_to_test, model.cv_values_.mean(axis=0))\n",
    "# 选取的岭系数值的位置\n",
    "plt.plot(model.alpha_, min(model.cv_values_.mean(axis=0)),'ro')\n",
    "plt.show()\n",
    "model.predict(x_data[2,np.newaxis])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建模型\n",
    "model = linear_model.LassoCV()\n",
    "model.fit(x_data, y_data)\n",
    "print(model.alpha_)# lasso系数\n",
    "print(model.coef_)# 相关系数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "弹性网(Elastic Net):\n",
    "+ 一般式： $J(\\theta )=\\frac{1}{2m}\\left [\\sum\\limits_{i=1}^m(h_\\theta(x^{(i)})-y^{(i)})^2+\\lambda \\sum\\limits_{j=1}^{m} \\left |\\theta _j \\right |^q \\right ]$\n",
    "+ 弹性网：$\\lambda \\sum\\limits_{j=1}^{m}(\\alpha \\theta _j^2+(1-\\alpha ) \\left |\\theta _j \\right |)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}